import json
import scrapy
from testPro.items import HuaItem_Price, HuaItem_Famliy, HuaQuota, HuaQuotaMap
import testPro.utils.RegularExpression as RE

regionDic = {

    "cn-north-4":"华北-北京四",
    "cn-north-9":"华北-乌兰察布一",
    "cn-east-3":"华东-上海一",
    "cn-east-2":"华东-上海二",
    "cn-south-1":"华南-广州",
    "cn-south-4":"华南-广州-友好用户换机",
    "cn-southwest-2":"西南-贵阳一",
    "ap-southeast-1":"中国-香港",
    "ap-southeast-3":"亚太-新加坡",
    "ap-southeast-2":"亚太-曼谷",
    "sa-brazil-1":"拉美-圣保罗一",
    "la-south-2":"拉美-圣地亚哥",
    "na-mexico-1":"拉美-墨西哥城一",
    "la-north-2":"拉美-墨西哥城二",
    "af-south-1":"非洲-约翰内斯堡",
    "eu-west-0":"欧洲-巴黎"

}

class HuaSpider_1(scrapy.Spider):
    name = 'hua-price'
    allowed_domains = ['www.huaweicloud.com']
    def __init__(self, category=None, *args, **kwargs):
        super(HuaSpider_1, self).__init__(*args, **kwargs)
        with open("./testPro/conf/config.json",'r') as load_f1:
            self.config = json.load(load_f1)
        with open("./testPro/conf/data.json",'r') as load_f2:
            self.table = json.load(load_f2)
        self.start_urls = self.config[self.name]["urlList"]
    # 解释器
    # instanceType = scrapy.Field()
    # vCpu = scrapy.Field()
    # memory = scrapy.Field()
    # minute_price = scrapy.Field()
    # hour_price = scrapy.Field()
    # month_price = scrapy.Field()
    # year_price = scrapy.Field()
    # two_yearPrice = scrapy.Field()
    # five_yearPrice = scrapy.Field()
    # Local_storage_GiB = scrapy.Field()
    def parse(self, response):
        tables = json.loads(response.xpath("//p/text()").extract()[0])
        region = regionDic[tables["region"]]
        for product in tables["product"]["ec2_vm"]:
            item = HuaItem_Price()
            item["region"] = region
            item["instanceType"] = '-'
            item["vCpu"] = '-'
            item["memory"] = '-'
            item["minute_price"] = '-'
            item["hour_price"] = '-'
            item["month_price"] = '-'
            item["year_price"] = '-'
            item["two_yearPrice"] = '-'
            item["three_yearPrice"] = '-'
            item["instanceType"] = product["spec"]
            item["vCpu"] = RE.find_num(product["cpu"])
            item["memory"] = RE.find_num(product["mem"])
            # item["minute_price"] = product["mem"]
            hasPri = False
            for pri in product["planList"]:
                if pri["billingMode"] == "ONDEMAND":
                    item["hour_price"] = pri["amount"]
                    hasPri = True
                if pri["billingMode"] == "MONTHLY":
                    item["month_price"] = pri["amount"]
                    hasPri = True
                if pri["billingMode"] == "YEARLY" and pri["periodNum"] == 1:
                    item["year_price"] = pri["amount"]
                    hasPri = True
                if pri["billingMode"] == "YEARLY" and pri["periodNum"] == 2:
                    item["two_yearPrice"] = pri["amount"]
                    hasPri = True
                if pri["billingMode"] == "YEARLY" and pri["periodNum"] == 3:
                    item["three_yearPrice"] = pri["amount"]
                    hasPri = True
            if hasPri:
                yield item
            else:
                continue

class HuaSpider_2(scrapy.Spider):
    name = 'hua-family'
    allowed_domains = ['www.huaweicloud.com']
    def __init__(self, category=None, *args, **kwargs):
        super(HuaSpider_2, self).__init__(*args, **kwargs)
        with open("./testPro/conf/config.json",'r') as load_f1:
            self.config = json.load(load_f1)
        with open("./testPro/conf/data.json",'r') as load_f2:
            self.table = json.load(load_f2)
        self.start_urls = self.config[self.name]["urlList"]
    
    def caseCPU(self, val):
        return ("处理器"  in val)
    def caseProp(self, val):
        return  "内存配比" in val
    def caseDisk(self, val):
        return  "硬盘" in val
    def caseClockSpeed(self, val):
        return  "基频/睿频" in val
    def caseNet(self, val):
        return  ("网络" in val and "带宽" in val)
    # 解释器
    def parse(self, response):
        section = response.xpath("//div[@class=\"section\"]")[0]

        tbody = section.xpath("./div/table/tbody/tr")
        thead = [x.extract() for x in section.xpath("./div/table/thead/tr/th/p/text()")]
        for line in tbody:
            item = HuaItem_Famliy()
            item["Family"] = '-'
            item["CPU"] = '-'
            item["ClockSpeed"] = '-'
            item["Proportion"] = '-'
            item["Usescenes"] = '-'
            for i,cell in enumerate(line.xpath("./td")):
                if i==0:
                    item["Family"] = RE.find_unchinese(cell.xpath("string(.)").extract()[0])
                elif i==1:
                    for li in cell.xpath("./ul/li/text()").extract():
                        if self.caseCPU(li):
                            item["CPU"] = li.split("：")[1]
                        elif self.caseProp(li):
                            item["Proportion"] = li.split("：")[1]
                        elif self.caseClockSpeed(li):
                            item["ClockSpeed"] = RE.fing_ClockSpeed(li)
                if "T6" in item["Family"]:
                    us = section
                else:
                    us = response.xpath("//div[@class=\"section\"]")[1:]
                for j in us.xpath("./p"):
                    if "适用场景" in j.xpath("string(.)").extract()[0]:
                        try:
                            item["Usescenes"] = j.xpath("./following-sibling::ul[1]").xpath("string(.)").extract()[0]
                        except:
                            item["Usescenes"] = j.xpath("./following-sibling::p[1]").xpath("string(.)").extract()[0]
                # if "T6" in item["Family"]:
                #     item["Usescenes"] = section.xpath("./following-sibling::ul[1]").xpath("string(.)").extract()[0]
            if 't6' in item["Family"]:
                continue
            yield item


class HuaSpider_3(scrapy.Spider):
    name = 'hua-family-details'
    allowed_domains = ['www.huaweicloud.com']

    def __init__(self, category=None, *args, **kwargs):
        super(HuaSpider_3, self).__init__(*args, **kwargs)
        with open("./testPro/conf/config.json",'r') as load_f1:
            self.config = json.load(load_f1)
        with open("./testPro/conf/data.json",'r') as load_f2:
            self.table = json.load(load_f2)
        self.start_urls = self.config[self.name]["urlList"]

    # 解释器
    def parse(self, response):
        tables = response.xpath("//table")
        for table in tables:
            thead = [x.extract() for x in table.xpath("./thead/tr/th/p/text()")]
            tbody = table.xpath("./tbody")
            print(thead)
            for line in tbody.xpath("./tr"):
                item = HuaQuota()
                for value in HuaQuotaMap.values():
                    item[value] = '-'
                for index,cell in enumerate(line.xpath("./td/p/text()").extract()):
                    item[HuaQuotaMap[thead[index]]] = cell
                yield item